const Base = require('./base.js');

module.exports = class extends Base {
  indexAction() {
    return this.display();
  }

  async addAction(){
    return this.display();
  }

  /**
   * 保存分类
   */
  async saveAction(){
    let data = this.post();
    if(think.isEmpty(data.id)){
      //保存,添加分类
      let res = await this.model('thinkjsplus_category').add(data);
      if(res){
        this.json({
          "success":true
        });
      }else{
        this.json({"success":false});
      }
    }else{
      //更新
      let res = await this.model("thinkjsplus_category").update(data);
      if(res){
        this.json({"success":true});
      }else{
        this.json({"success":false});
      }
    }
  }
  /**
   * 查看分类,商品分类列表
   */
  async listAction(){
    //实例化模型
    const user = this.model('thinkjsplus_category');
    const data = await user.select();
    return this.json(data);
  }

  /**
   * 删除
   */
  async delAction(){
    let categoryModel = this.model("thinkjsplus_category");
    let posts = this.post("id");
    let delNums = categoryModel.where({id:['IN',posts]}).delete();
    if(delNums){
      this.json({"success":true});
    }else{
      this.json({"success":false});
    }
  }
};